package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.SetMealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.CheckGroupVO;
import com.itheima.health.vo.SetmealVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author 十一
 * @Description
 * @create 2020-11-06-18:53
 */
@Service
@Transactional
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {

    @Autowired
    private CheckGroupService checkGroupService;

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //创创建条件构造器
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        Page<Setmeal> page=null;
        if(StringUtils.isBlank(queryPageBean.getQueryString())){
            //普通查询
            page = page(new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }else{
            //条件查询
            wrapper.like("name",queryPageBean.getQueryString());
            wrapper.or();
            wrapper.like("code",queryPageBean.getQueryString());
            wrapper.or();
            wrapper.like("helpCode",queryPageBean.getQueryString());
            page=page(new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),wrapper);
        }
        return  new PageResult(page.getTotal(), page.getRecords());
    }

    //新增或修改
    @Override
    public Boolean addOrUpdate(SetmealDTO setmealDTO) {

        //判断是添加操作还是修改操作
        Integer id = setmealDTO.getId();
        if(StringUtils.isNotBlank(String.valueOf(id))){
            //修改操作，删除中间表中的数据
            baseMapper.deleteCheckGroupBySetMealId(id);
            if(setmealDTO.getImg() != null){
                //删除套餐表图片信息
                baseMapper.deleteImgById(id);
            }
        }
        //添加信息到SetMeal表
        boolean flag = saveOrUpdate(setmealDTO);
        Integer mid = setmealDTO.getId();
        //如果gid值存在表示执行的是新增操作gid
        Integer[] checkgroupIds = setmealDTO.getCheckgroupIds();
        if(checkgroupIds != null && checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                //想中间表添加数据
                baseMapper.addCheckGroupInfo(mid,checkgroupId);
            }
        }
        //用户添加预约套餐，删除set中同名图片文件
        RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,setmealDTO.getImg());
        return flag;
    }

    //定时清理垃圾图片--由xxl-job任务调度
    @Override
    public void clearOssImg(){
        //获取Redis中set集合中的所有元素
        Set<String > membersOfSet = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        //判断集合是否为空
        if(membersOfSet.size() != 0){
            //遍历集合
            for (String uuidFileName : membersOfSet) {
                //判断Redis中是否存在相同值的key-value
                boolean existsKey = RedisUtil.existsKey(RedisConstant.SINGLE_PIC + uuidFileName);
                System.out.println("existsKey = " + existsKey);
                if(existsKey == false){
                    //删除阿里云中的垃圾图片
                    AliyunUtils.deleteFile(uuidFileName);
                    System.out.println("从阿里云中删除了垃圾图片："+uuidFileName);
                    //删除set集合中的文件
                    RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,uuidFileName);
                }
            }
        }
    }

    //查询套餐详细信息
    @Override
    public SetmealVO findSetMealDetail(int id) {
        //1、查询套餐信息
        SetmealVO setmealVO = baseMapper.findSetMealDetail(id);
        //2、查询套餐中的检查组信息
        List<CheckGroupVO> checkGroupVOList = baseMapper.findCheckGroupsBySetMealId(id);
        //3、查询检查组中的检查项信息
        for (CheckGroupVO checkGroupVO : checkGroupVOList) {
            List<CheckItem> checkItemList = baseMapper.findCheckItemByCheckGroupId(checkGroupVO.getId());
            checkGroupVO.setCheckItemList(checkItemList);
        }
        setmealVO.setCheckGroups(checkGroupVOList);
        return setmealVO;
    }

    //查询套餐基本信息
    @Override
    public Setmeal findSetMealById(int id) {
        return baseMapper.findSetMealById(id);
    }

    //查询套餐检查组信息
    @Override
    public SetmealVO findCheckGroupsBySetMealId(int id) {
        //查询所有套餐
        List<CheckGroup> checkGroupList = checkGroupService.list();
        //查询套餐对应的检查组id
        int[] checkGroupIds = baseMapper.findCheckGroupIdsBySetMealId(id);
        SetmealVO setmealVO = new SetmealVO();
        setmealVO.setCheckGroupList(checkGroupList);
        setmealVO.setCheckGroupIds(checkGroupIds);
        return setmealVO;
    }

    //删除套餐信息
    @Override
    public Boolean deleteSetMeal(int id) {
        return baseMapper.deleteSetMeal(id);
    }

    //查询所有套餐
    @Override
    public List<Setmeal> findAllSetMeal() {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        return baseMapper.selectList(wrapper);
    }

}
